import { ListView } from 'react-native'
import { Toast } from 'antd-mobile'
import * as _ from 'lodash'
import { RefreshState } from '../../widgets'
import { createAction, NavigationActions } from '../../utils/index'
import * as consultantService from '../../services/consultant/consultantService'
import * as Libs from '../../utils/Libs'

const initState = {
  innerData:[],
  currentPage: 1,
  resetData: false,
  dataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }).cloneWithRows([]),
  secondList: [],
  secondInnerData: [],
  secondCurrentPage: 1,
  secondDataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }).cloneWithRows([]),
  submitResultState: 0,
  secondResetData: false,
}

export default {
  namespace: 'consultant',
  state: initState,
  reducers: {
    initState(state, { payload }) {
      return {...initState}
    },
    modifyState(state, {payload}) {
      return {...state, ...payload}
    },
    resetOneDetail(state, {payload}) {
      return {...state,
        innerData: [],
        currentPage: 1,
        dataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }).cloneWithRows([]),}
    },
    resetTwoDetail(state, {payload}) {
      return {...state,
        secondInnerData: [],
        secondCurrentPage: 1,
        secondDataSource: new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 }).cloneWithRows([]),}
    },
    resetStatus(state, {payload}) {
      return {
        ...state, submitResultState: 0
      }
    }
  },
  effects: {
    *getSecondDetail({payload}, {call, select, put}) {
      const state = yield select(state => state.consultant)
      const resp = yield call(consultantService.getConsultantDetail, {
        currentPage: state.secondCurrentPage,
        type: payload.type,
        projectId: payload.projectId
      })
      if (resp.resultCode !== 200) {
        payload.listView.endRefreshing(RefreshState.NoMoreData)
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data.data || []
      console.log('resp is', resp)
      if(respData.length === 0) {
        payload.listView.endRefreshing(RefreshState.NoMoreData)
      } else {
        const newD = state.secondInnerData.concat(respData)
        yield put(createAction("modifyState")({
          secondInnerData: newD,
          secondDataSource: state.secondDataSource.cloneWithRows(newD),
          secondCurrentPage: state.secondCurrentPage + 1
        }))
        payload.listView.endRefreshing(RefreshState.Idle)
      }
    },
    *getFirstDetail({ payload }, { call, select, put}) {
      console.log('fetch first detail')
      const state = yield select(state => state.consultant)
      const resp = yield call(consultantService.getConsultantDetail, {
        currentPage: state.currentPage,
        type: payload.type,
        projectId: payload.projectId
      })
      if (resp.resultCode !== 200) {
        payload.listView.endRefreshing(RefreshState.NoMoreData)
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data.data || []
      console.log('resp is', resp)
      if(respData.length === 0) {
        payload.listView.endRefreshing(RefreshState.NoMoreData)
      } else {
        const newD = state.innerData.concat(respData)
        yield put(createAction("modifyState")({
          innerData: newD,
          dataSource: state.dataSource.cloneWithRows(newD),
          currentPage: state.currentPage + 1
        }))
        payload.listView.endRefreshing(RefreshState.Idle)
      }
    },
    *getSecondList({payload}, {call, select, put}) {
      const params = {}
      const resp = yield call(consultantService.getSecondList, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respD = resp.data
      yield put(createAction("modifyState")({
        secondList: respD
      }))
    },
    *submitMessage({payload}, { call, select, put} ){
      const params = _.clone(payload)
      const resp = yield call(consultantService.submitMessage, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      } else {
        yield put(createAction("modifyState")({
          submitResultState: 1,
        }))
        const type = payload.type
        if(type == 0) {
          yield put(createAction("modifyState")({
            resetData: true
          }))
        } else if(type == 2) {
          yield put(createAction("modifyState")({
            secondResetData: true
          }))
        }
      }
    }
  },
  subscriptions: {
  },
}
